package org.ring.common.transport;

import io.netty.buffer.ByteBuf;
import io.netty.channel.ChannelHandlerContext;
import io.netty.handler.codec.MessageToByteEncoder;

/**

 * @since 2023-10-27 11:26
 */
public class NettyEncoder extends MessageToByteEncoder {

    private Codec codec;

    public NettyEncoder(Codec codec){
        this.codec = codec;
    }

    @Override
    protected void encode(ChannelHandlerContext ctx, Object msg, ByteBuf out) throws Exception {
        if(msg instanceof ByteBuf){
            out.writeBytes((ByteBuf) msg);
            return;
        }

        codec.encode(msg,out);
    }


}
